<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="产品内容" prop="username">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入产品内容"
            clearable
          />
        </el-form-item>
        <el-form-item label="产品链接" prop="productUrl">
          <el-input
            v-model="queryParams.productUrl"
            placeholder="请输入产品链接"
            clearable
          />
        </el-form-item>
        <el-form-item label="是否上架" prop="isSlive">
          <el-select v-model="queryParams.isSlive" placeholder="是否上架" clearable>
            <el-option 
                v-for="dict in dict.type.adv_products_is_slive"
                :label="dict.label" 
                :value="dict.value"
                :key="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:role:add']"
          >新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:role:edit']"
          >修改</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete2"
            v-hasPermi="['system:role:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:role:export']"
          >导出</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-download"
            size="mini"
            @click="downloadSome"
            v-hasPermi="['system:role:export']"
          >批量下载</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
  
      <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="产品图片" align="center" prop="productImage" >
          <template slot-scope="scope">
            <image-preview :src="scope.row.productImage" :width="50" :height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="产品详细" align="center" prop="productDesc" />
        <el-table-column label="视频地址" align="center" prop="productUrl">
          <template slot-scope="scope">
            <video width="200" controls :height="100">
              <source :src="baseUrl + scope.row.productUrl" type="video/mp4">
              Your browser does not support the video tag.
            </video>
          </template>
        </el-table-column>
        <el-table-column label="产品单价" align="center" prop="productPrice" />
        <el-table-column label="是否上架" align="center" prop="isSlive">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.adv_products_is_slive" :value="scope.row.isSlive"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:role:edit']"
            >修改</el-button>
            <el-button
            size="mini"
            type="text" 
            icon="el-icon-edit"
            @click="$download.resource(scope.row.productUrl)"
            >下载视频</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:role:remove']"
            >删除</el-button>
            <!-- <el-button
              size="mini"
              type="text"
              icon="el-icon-bottom"
              @click="handleSolid(scope.row)"
              v-hasPermi="['system:role:remove']"
            >入库登记</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-top"
              @click="handleStop(scope.row)"
              v-hasPermi="['system:role:remove']"
            >出库登记</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"/>
  
      <!-- 添加或修改用户和角色关联对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="form.productName" placeholder="请输入产品名称" clearable />
          </el-form-item>
          <el-form-item label="封面图片" prop="productImage">
            <image-upload v-model="form.productImage" limit="1"/>
          </el-form-item>
          <el-form-item label="产品详细" prop="productDesc">
            <el-input type="textarea" v-model="form.productDesc" placeholder="请输入产品详细"></el-input>
          </el-form-item>
          <el-form-item label="视频上传" prop="productUrl">  
              <file-upload v-model="form.productUrl" @fileSize="handleFileSize"/>
          </el-form-item> 
          <el-form-item label="文件大小" prop="videoSize">
            <el-input  v-model="form.videoSize"  disabled></el-input>
            </el-form-item> 
          <el-form-item label="下载地址" prop="productDownloadUrl" hidden>
            <el-input type="textarea" v-model="form.productDownloadUrl" placeholder="请输入下载地址" hidden></el-input>
            </el-form-item> 
          <el-form-item label="产品单价" prop="productPrice" >
            <el-input v-model="form.productPrice" placeholder="请输入产品单价" style="width: 220px;" clearable />
          </el-form-item>
          <el-form-item label="是否上架" prop="isSlive">
            <el-select v-model="form.isSlive" placeholder="请选择是否上架" clearable>
              <el-option 
                v-for="dict in dict.type.adv_products_is_slive"
                :label="dict.label" 
                :value="dict.value"
                :key="dict.value"/>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import request from '@/utils/request'
 import { listProducts, addProducts, updateProducts, delProducts} from '@/api/server/products.js'
export default {
  dicts: ['adv_products_is_slive'],
    data() {
      return {
        choseListUrl: [], // 选择的数据(下载地址)
        productType: 2, // 表示当前页是内容产品
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          productType: 2,
          productName: '',
          productModel: '',
          productDesc: '',
          productUrl: '',
          productPrice: '',
          isSlive: '',
          createTime: '',
          stocknum: '',
        },
        showSearch: true,
        single: false,
        multiple: false,
        loading: false,
        currentPage4: 1,
        open1: false,
        open2: false,
        roleList: [],
        total: 0,
        title: "",
        open: false,
        form: {},
        rules: {
        productName: [
          { required: true, message: "请输入产品名称", trigger: "blur" }
        ],
        productModel: [
          { required: true, message: "请输入产品型号", trigger: "blur" }
        ],
        productDesc: [
          { required: true, message: "请输入产品明细", trigger: "blur" }
        ],
        productUrl: [
          { required: true, message: "请输入产品链接", trigger: "blur" }
        ],
        productPrice: [
          { required: true, message: "请输入产品单价", trigger: "change" }
        ],
        isSlive: [
          { required: true, message: "请选择是否上架", trigger: "change" }
        ],
        stocknum: [
          { required: true, message: "请输入产品库存", trigger: "change" }
        ]
      },
      baseUrl: process.env.VUE_APP_BASE_API
      }
    },
    created() {
      this.getList();
    },
    methods: {
      handleFileSize(fileSizes) {
        this.form.videoSize = fileSizes[0]; // 假设只上传一个文件
      },
      handleDelete2(){
      this.$confirm('此操作将永久删除该内容, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log("this.ids--->",this.ids);
        delProducts(this.ids.join(',')).then((response) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getList();
        }).catch((error) => {
          this.$message({
            type: 'error',
            message: '删除失败!'
          });
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
      // 批量下载
      downloadSome() {
        console.log("选择的数据(下载地址): ", this.choseListUrl)
        request({
          url: '/download/bulk',
          method: 'POST',
          data: this.choseListUrl
        }).then(response => {
          console.log(response)
          this.$download.resource(response.data)
        })
      },
      handleQuery() {
        this.getList();
      },
      resetQuery() {},
      handleExport() {
        this.download('/system/products/export', {
        ...this.queryParams
      }, `内容产品_${new Date().getTime()}.xlsx`)
      },
      getList() {
        listProducts(this.queryParams).then((response) => {
          this.roleList = response.rows;
          this.total = response.total;
        })
      },
      cancel() {
        this.open = false;
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
      },
      handleSelectionChange(selection) {
        this.choseListUrl = selection.map(item => item.productUrl);
        this.ids = selection.map(item => item.advProductsId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加产品";
      },
      handleUpdate(row) {
        this.reset();
        this.form = row;
        this.open = true;
        this.title = "修改产品";
        console.log("MyData------>"+baseUrl+row.productUrl)
      },
      reset() {
        this.form = {
          nickname: "",
          username: "",
          parentUsername: "",
          status: "",
          isAnchor: ""
        }
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除该内容, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delProducts(row.advProductsId).then((response) => {
            this.getList();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if(this.title === '添加产品') {
            this.form.productType = this.productType;
            this.form.productDownloadUrl = this.form.productUrl;
            addProducts(this.form).then((response) => {
              this.getList();
              this.$message({
                type: 'success',
                message: '提交成功!'
              });
              this.open = false;
            })
          }
          if(this.title === '修改产品') {
            updateProducts(this.form).then((response) => {
              this.getList();
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
              this.open = false;
            })
          }
        } else {
          this.$message({
            type: 'error',
            message: '表单验证失败!'
          });
        }
      })
    },
      handleSolid(row) {
        this.open1 = true;
        this.title = "入库登记";
      },
      handleStop(row) {
        this.open2 = true;
        this.title = "出库登记";
      },
      cancel1() {
        this.open1 = false;
      },
      cancel2() {
        this.open2 = false;
      },
      
    }
  }
  </script>
  
  <style>
  
  </style>
  
  